
import axios from 'axios'
import { MessageBox } from 'element-ui'

function getLocation (getCity, self) {
  let lable = `<span>${getCity.nm}</span>`
  MessageBox.confirm(`系统定位您在${lable}，是否切换？`, '提示', {
    customClass: 'box',
    showClose: false,
    dangerouslyUseHTMLString: true,
    showCancelButton: true,
    confirmButtonText: '立即切换',
    cancelButtonText: '暂不切换',
    closeOntapModal: false
  }).then(() => {
    console.log('切换成功', getCity)
    self.$store.commit('getCurrentCity', getCity)
  }).catch(() => {
    console.log('取消切换')
  })
};
/**
 *  首次创建实例, 本地存储位置 与定位 不等，调用弹窗
 *  第一次弹窗后，设置code 直至实例销毁不在弹出组件
 */
function localtionAxios (self, localCity) {
  axios('/api/getLocation').then(res => {
    let getCity = res.data.data
    if (localCity.nm !== getCity.nm) {
      getLocation(getCity, self)
      // window.localStorage.setItem('localCode', 1)// 定位切换弹窗记录
    }
  })
}

export default localtionAxios
